<?php 
	require_once ('Database.php');
	require_once ('News.php');
	require_once ('checks.php');
	$db=new DB();
	$arr = $db->findName("articles");
	$n= count($arr);
	
	
	
	if (isset($_POST['go']) && isset($_POST['art_name'])) {
	
		if (!isset($_POST['newsvisible'])) {
			$flag=0;
		} else {
			$flag=1;
		}
			
		$news= new News($_POST['news_name'],$_POST['news'],$_GET['image'],$_POST['order'],$flag);
		$news->setId($_POST['art_name']);
		$db->addNews($news);
		header('Location: ?page=news_image&references=admin_add_news');
    }
?>



	<form method="post" action="?page=admin_add_news<?php if (isset($_GET['image'])) echo "&image=".$_GET['image']; ?>">
		<table>
			<tr>
				<td><label for="art_name">Article Name : </label>
				</td>
				<td><select name="art_name" id="art_name">
						<?php
							
							for ($i=0;$i<$n;$i++) {
								echo "<option value='$arr[$i]'>$arr[$i]";
							}
						?>
				</select>
				</td>
			</tr>
			<tr>
				<td><label for="newsvisible">Visible : </label>
				</td>
				<td><input type="checkbox" name="newsvisible" id="newsvisible">
				</td>
			</tr>
			<tr>
				<td><label for="order">Order : </label>
				</td>
				<td><input type="text" name="order" id="order" size="2" value="<?php echo $db->lastNewsId()+1; ?>">
				</td>
			</tr>
			<tr>
				<td><label for="news_name">News Name : </label>
				</td>
				<td><input type="text" name="news_name" id="news_name">
				</td>
			</tr>
			<tr>
				<td><label for="news_image"> Image : </label>
				</td>
				<td>
				<?php 
					if (!isset($_GET['image'])) {
						
						echo "<img src='images/gallery/no_photo.jpg' width='300' height='150'>";
						
					} else {
						echo $_GET['image'];
						echo "<br/><img src='images/gallery/".$_GET['image']."' width='300' height='150'>";
					}
					echo "<br/><input type='button' value='Choose Image' onclick=\"location.href='?page=news_image'\">";
				?>
				</td>
			</tr>
			<tr>
				<td style="vertical-align: top;"><label for="news">News : </label>
				</td>
				<td>
					<textarea name="news" id="news" cols="50" rows="10">
						
					</textarea>
				</td>
			</tr>

			<tr>
				<td colspan="2">
				<?php 
					if (isset($_GET['image'])) {
						echo "<input type=\"submit\" value=\"Save\" name=\"go\">"; 
					}
				?>
				</td>
			</tr>
		</table>
	</form>


